﻿using Microsoft.Phone.Net.NetworkInformation;
using SpaceNeedle.SDK.Windows.Platform.Commands;
using SpaceNeedle.SDK.Windows.Platform.Data;
using SpaceNeedle.SDK.WindowsPhone;
using SpaceNeedle.x6473.WindowsPhone.Common;
using SpaceNeedle.x6473.WindowsPhone.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.Networking.Connectivity;

namespace SpaceNeedle.x6473.WindowsPhone.ViewModels
{
    public class GlobalViewModel : ViewModelBase
    {
        public GlobalViewModel() : base()
        {
            this.AboutCommand = new RelayCommand(About);
            this.UploadsCommand = new RelayCommand(Uploads);
            this.SignOutCommand = new RelayCommand(SignOut);
            this.ClearCacheCommand = new RelayCommand(ClearCache);
            this.ShowNetworkStatusInfoCommand = new RelayCommand(ShowNetworkStatusInfo);

            App.NetworkService.StatusChanged += NetworkService_StatusChanged;

            this.IsConnected = App.NetworkService.IsConnected;
        }

        void NetworkService_StatusChanged(object sender, EventArgs e)
        {
            this.IsConnected = App.NetworkService.IsConnected;
        }

        public AppContent AppContent
        {
            get { return this.Get<AppContent>(); }
            set { this.Set(value); }
        }

        public bool IsConnected
        {
            get { return this.Get<bool>(); }
            set { this.Set(value); }
        }

        public ICommand AboutCommand { get; set; }
        public ICommand UploadsCommand { get; set; }
        public ICommand SignOutCommand { get; set; }
        public ICommand ClearCacheCommand { get; set; }
        public ICommand ShowNetworkStatusInfoCommand { get; set; }

        protected virtual void ShowNetworkStatusInfo(object param)
        {
            AppServices.Current.DialogService.Show("A sua conectividade com a Internet não está funcionando adequadamente no momento. É possível que os dados que esteja visualizando não estejam devidamente atualizados. Por favor, verifique a sua conexão.", this.AppContent.Name);

            Tracking.SendEvent("Informação", "Rede", "Informação de Conexão Lida", 0);

        }

        private void About(object param)
        {
            AppServices.Current.NavigationService.NavigateDirect(ViewsUris.About);
        }

        private void Uploads(object param)
        {
            AppServices.Current.NavigationService.NavigateDirect(ViewsUris.Uploads);
        }

        private void SignOut(object param)
        {
            AppServices.Current.NavigationService.NavigateDirect(ViewsUris.Auth);
        }

        protected async void ClearCache(object param)
        {
            this.IsInProgress = true;

            await this.ClearCacheAsync();
            
            this.IsInProgress = false;
        }

        protected async Task ClearCacheAsync()
        {
            if (!this.IsConnected)
            {
                return;
            }
            else
            {
                await AppViewModel.Instance.Cache.Delete(CacheKeys.Phones);
                await AppViewModel.Instance.Cache.Delete(CacheKeys.Alerts);
                await AppViewModel.Instance.Cache.Delete(CacheKeys.Requests);
                await AppViewModel.Instance.Cache.Delete(CacheKeys.AllRequests);
                await AppViewModel.Instance.Cache.Delete(CacheKeys.UserRequests);
                await AppViewModel.Instance.Cache.Delete(CacheKeys.AppContent);
            }
        }

        protected async override void Load()
        {
            await this.LoadAsync();
        }

        protected async Task LoadAsync()
        {
            AppViewModel.Instance.Properties.AppContent = await AppViewModel.Instance.LoadAppContent();

            this.AppContent = AppViewModel.Instance.Properties.AppContent;
        }
    }
}
